package org.example.code.test2024.eight_mouth;

import java.util.*;

public class text {
    /*
    小红所有的关键词。每行输入一个。你需要按照搜索频次从高到低输出。频次相同的，你需要按字典序升序输出。
     */

    public static void text1(String[] args) {
        Scanner in = new Scanner(System.in);
        HashMap<String ,Integer> map = new HashMap<String, Integer>();
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String word = in.nextLine();
            if (map.containsKey(word)) {
                map.put(word, map.get(word) + 1);
            }
            else {
                map.put(word, 1);
            }
        }

        List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
        list.sort(new Comparator<Map.Entry<String, Integer>>() {
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                if (!o1.getValue().equals(o2.getValue())) {
                    return o2.getValue() - o1.getValue();
                } else {
                    return o1.getKey().compareTo(o2.getKey());
                }
            }
        });

        for (Map.Entry<String, Integer> entry : list) {
            if(entry.getValue() >= 3)
                System.out.println(entry.getValue());
            }

        in.close();

    }

    public String[] specialNumber (int n) {
        // write code here
        List<String> list = new ArrayList<>();
        for(int i=1;i<=n;i++){
            if(i%3==0&&i%5==0){
                list.add("Nowcoder");
            }else if(i%3==0){
                list.add("Now");
            }else if(i%5==0){
                list.add("coder");
            }else{
                list.add(String.valueOf(i));
            }
        }
        return list.toArray(new String[0]);
    }
    public boolean isBalanced (TreeNode root) {
        // write code here
        if(root==null){
            return true;
        }
        TreeNode left = root.left;
        TreeNode right = root.right;
        int abs = Math.abs(dep(left)-dep(right));
        if(abs>1)
            return false;
        return isBalanced(root.left)&&isBalanced(root.right);
    }

    private int dep(TreeNode root) {
        if(root==null)
            return 0;
        int left = dep(root.left)+1;
        int right = dep(root.right)+1;
        return Math.max(right, left);
    }


    public int longestUniqueSubstring (String s) {
        // write code here
        List<Character> list = new ArrayList<>();
        char []str = s.toCharArray();
        int max = 0;
        for(int i=0;i<s.length();i++){
            if (list.contains(str[i])) {
                max = Math.max(max,list.size());
                list.clear();
            }else{
                list.add(str[i]);
            }
        }
        return Math.max(max,list.size());
    }

    public static boolean isValidParentheses (String s) {
        // write code here
        char []str = s.toCharArray();
        Stack<Character> stack = new Stack<>();
        for(int i=0;i<str.length;i++){
            if(str[i]=='('||str[i]=='['||str[i]=='{'){
                stack.push(str[i]);
            }else{
                if(str[i]==')'&&stack.peek()=='(')
                    stack.pop();
                if(str[i]==']'&&stack.peek()=='[')
                    stack.pop();
                if(str[i]=='}'&&stack.peek()=='{')
                    stack.pop();
            }
        }
        return stack.isEmpty();
    }
    public int countUniqueElements (int[] nums) {
        // write code here
        Set<Integer> set = new HashSet<>();
        for(int i=0;i<nums.length;i++){
            set.add(nums[i]);
        }
        Integer[] array = set.toArray(set.toArray(new Integer[0]));
        return array.length;
    }
    public ListNode ReverseList (ListNode head) {
        // write code here
       ListNode node = null;
       while(head !=null){
           ListNode next = head.next;
           head.next = null;
           node = head;
           head = next;
       }

       return node;
    }

    private void printNode(ListNode node){
        if(node ==null){
            System.out.println("链表为空");
        }
        while(node!=null){
            System.out.print(node.val);
            node = node.next;
        }
        System.out.println();
    }

    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int t = nums2.length +nums1.length- n;
        for(int i=m;i<nums1.length;i++){
            if(n==0)
                break;
            nums1[m]=nums2[t];
            n--;
        }
        Arrays.sort(nums1);
        for(int i=0;i<nums1.length;i++){
            System.out.println(nums1[i]);
        }
    }


    public List<Double> averageOfLevels(TreeNode root) {
        List<Integer> count = new ArrayList<>();
        List<Double> sums = new ArrayList<>();
        List<Double> averages = new ArrayList<>();
        dfs(root,0,sums,count);
        for(int i=0;i<sums.size();i++){
            averages.add(sums.get(i)/count.get(i));
        }
        return averages;
    }

    private void dfs(TreeNode root, int i, List<Double> sums, List<Integer> count) {
        if(root == null){
            return;
        }
        if(i>sums.size()){
            sums.set(i, Double.valueOf(root.val));
            count.set(i,0);
        }else{
            sums.set(i,sums.get(i)+root.val);
            count.set(i,count.get(i)+1);
        }
    }

    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> sum = new ArrayList<>();
        if(root==null)
            return sum;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            List<Integer> level = new ArrayList<>();
            int queueSize = queue.size();
            for (int i = 0; i < queueSize; i++) {
                TreeNode node = queue.poll();
                level.add(node.val);
                if(node.left!=null){
                    queue.offer(node.left);
                }
                if(node.right!=null){
                    queue.offer(node.right);
                }
            }
            sum.add(level);
        }

        return sum;
    }

    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> sum = new ArrayList<>();
        if(root==null)
            return sum;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        boolean flag = false;
        while (!queue.isEmpty()) {
            Deque<Integer> level = new LinkedList<Integer>();
            int queueSize = queue.size();
            for (int i = 0; i < queueSize; i++) {
                TreeNode node = queue.poll();
                if(!flag){
                    level.offerLast(node.val);
                }else{
                    level.offerFirst(node.val);
                }
                if(node.left!=null){
                    queue.offer(node.left);
                }
                if(node.right!=null){
                    queue.offer(node.right);
                }
            }
            sum.add((List<Integer>) level);
            flag=!flag;
        }

        return sum;
    }


    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if(root==null)
            return list;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            List<Integer> level = new ArrayList<>();
            int queueSize = queue.size();
            for (int i = 0; i < queueSize; i++) {
                TreeNode node = queue.poll();
                level.add(node.val);
                if(node.left!=null){
                    queue.offer(node.left);
                }
                if(node.right!=null){
                    queue.offer(node.right);
                }
            }
            list.add(level.get(level.size()-1));
        }
        return list;
    }
    int min = 100005;
    int pre = -1;
    public int getMinimumDifference(TreeNode root) {
        if(root==null)
            return -1;
        fun(root);
        return min;
    }

    private void fun(TreeNode root) {
        if(root==null)
            return;
        fun(root.left);
        if(pre==-1){
            pre = root.val;
        }else{
            min = Math.min(min, root.val-pre);
            pre=root.val;
        }
        fun(root.right);
    }

    public int kthSmallest(TreeNode root, int k) {
        if(root==null)
            return -1;
        List<Integer> list = new ArrayList<>();
        fun2(root,list);
        return list.get(k);
    }

    private void fun2(TreeNode root, List<Integer> list) {
        if(root==null)
            return;
        list.add(root.val);
        fun2(root.left,list);
        fun2(root.right,list);
    }

    public boolean isValidBST(TreeNode root) {
        if(root==null)
            return true;
        return fun3(root,Integer.MAX_VALUE,Integer.MIN_VALUE);
    }

    private boolean fun3(TreeNode root, int maxValue, int minValue) {
        if(root==null)
            return true;
        if(root.val>minValue&&root.val<maxValue){
            return fun3(root.left,root.val,minValue)&&fun3(root.right,maxValue,root.val);
        }
        return false;
    }

    public int evalRPN(String[] tokens) {
        int ans = 0;
        Stack<Integer> stack = new Stack<>();
        for(int i=0;i<tokens.length;i++){
            if(tokens[i].equals("+")||tokens[i].equals("/")||
                    tokens[i].equals("-")|| tokens[i].equals("*")){
                if(stack.isEmpty())
                    continue;
                int x = stack.pop();
                if(stack.isEmpty())
                    continue;
                int y = stack.pop();
                switch (tokens[i]){
                    case "+":
                        ans = x+y;
                        break;
                    case "-":
                        ans = x-y;
                        break;
                    case "/":
                        ans = x/y;
                        break;
                    case "*":
                        ans = x*y;
                        break;
                }
                stack.push(ans);
            }else{
                int x = Integer.parseInt(tokens[i]);
                if(x>0&&x<201)
                    stack.push(x);
            }
        }
        return ans;
    }

    public String simplifyPath(String path) {
        String []str = path.split("/");
        Stack<String> stack = new Stack<>();
        for(int i=0;i<str.length;i++){
            System.out.println(str[i]);
           if(str[i].equals("..")){
               if(!stack.isEmpty())
                   stack.pop();
           }else if(str[i].equals("")||str[i].equals(".")){
               continue;
           }else{
               stack.push(str[i]);
           }
        }
        String result = "";
        if(stack.isEmpty()){
            return "/";
        }
        while(!stack.isEmpty()){
            result=result+stack.pop();
        }
        result = "/"+result;
        return result;
    }
    int []a = {0,0,1,-1,1,-1,1,-1};
    int []b = {1,-1,0,0,1,-1,-1,1};
    public void gameOfLife(int[][] board) {
        int m = board.length;
        int n = board[0].length;
        int [][] map = new int[m][n];
        for(int i=0;i<m;i++) {
            for (int j = 0; j < n; j++) {
                map[i][j]=board[i][j];
            }
        }
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(isLife(map,i,j)){
                    board[i][j]=1;
                }else{
                    board[i][j]=0;
                }
            }
        }
        for(int i=0;i<m;i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(board[i][j]+" ");
            }
            System.out.println("");
        }
    }
    private boolean isLife(int[][] board, int x, int y) {
        int m = board.length;
        int n = board[0].length;
        int k=0;
        for(int i=0;i<8;i++){
            int xx = x+a[i];
            int yy = y+b[i];
            if(xx>-1&&xx<m&&yy>-1&&yy<n&&board[xx][yy]==1){
                k++;
            }
        }
        if(k<2){
            return false;
        }
        if((k==2||k==3)&&board[x][y]==1)
            return true;
        if(k>3&&board[x][y]==1)
            return false;
        if(k==3&&board[x][y]==0)
            return true;
        return false;
    }

    public static void main(String[] args) {
        System.out.println(new text().simplifyPath("/home/path/"));
    }


}
